https://leetcode.cn/problems/longest-univalue-path/submissions/677012859/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
	int res;
public:
	int longestUnivaluePath(TreeNode* root) {
		res = 0;
		maxPath(root);
		return res;
	}
	int maxPath(TreeNode *root){
		if(root == nullptr) return 0;
		int left = maxPath(root->left) , right = maxPath(root->right);
		int left1 = 0 , right1 = 0;
		if(root->left && root->val == root->left->val) left1 = left + 1;
		if(root->right && root->val == root->right->val) right1 = right + 1;
		res = max(res , left1 + right1);
		return max(left1 , right1);
	}
};
